package com.store.app.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.store.app.BaseActivity;
import com.store.app.ExitApplication;
import com.store.app.R;
import com.store.app.utils.MoneyUtil;

import java.math.BigDecimal;

/**
 * Created by Ben
 * on 2016/8/14 0014.\
 * 现金收款页面
 */
public class PayByCashActivity extends BaseActivity {
    private TextView pay_one, pay_two, pay_three, pay_four, pay_five, pay_six, pay_seven, pay_eight, pay_nine, pay_zero, pay_space, pay_del;
    private TextView change, pay_affirm, pay_cancel, handleone, money;
    private String moneytext;
    private double price_original = 0;//原价
    private boolean donateIsShown,moreRewardDataIsEmpty,moreRewardIsEmpty;
    private PopupWindow popupWindowCoupon = null;
    private int moreRewardDataInt;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        ExitApplication.getInstance().addActivity(this);
        getWindow()
                .addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        setContentView(R.layout.cash_register);

        findViewById(R.id.title_layout).setPadding(0,getSysStatusH(),0,0);
        initIntentData();
        initView();
    }

    private int getSysStatusH(){
        int statusBarHeight = -1;
//获取status_bar_height资源的ID
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight = getResources().getDimensionPixelSize(resourceId);
        }
        return statusBarHeight;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(popupWindowCoupon!=null){
            popupWindowCoupon.dismiss();
        }
    }

    private void initIntentData() {
        price_original = getIntent().getDoubleExtra("price_original", 0.0);
    }

    private void initView() {

        pay_affirm = (TextView) findViewById(R.id.pay_affirm);
        ImageView returnIv = (ImageView) findViewById(R.id.retreat_dialog);

        pay_cancel = (TextView) findViewById(R.id.pay_cancel);
        pay_cancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                finish();
            }
        });

        findViewById(R.id.retreat_dialog).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        //找零钱
        change = (TextView) findViewById(R.id.change);
        //应付多少
        handleone = (TextView) findViewById(R.id.handleone);
        handleone.setText(MoneyUtil.formatMoney(price_original+"") + "");
        //实付多少
        money = (TextView) findViewById(R.id.money);
        money.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable s) {
            }

            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                if (s.toString().length() <= 0) {
                } else {
                    double residue1 = Double.parseDouble(s.toString());
                    double residue = residue1 - (price_original);
                    residue = (Math.round(residue * 100) / 100.0);
                    if (residue >= 0) {
                        change.setText(String.format("¥%.2f", residue));
                        pay_affirm.setBackgroundResource(R.drawable.border5px_blue_00c3e2);
                        pay_affirm.setTextColor(Color.parseColor("#ffffff"));
                    } else {
                        change.setText("¥ 0");
                        pay_affirm.setBackgroundResource(R.drawable.border_gray_80ffffff);
                        pay_affirm.setTextColor(Color.parseColor("#333333"));
                    }
                }

            }
        });

        pay_one = (TextView) findViewById(R.id.pay_one);
        pay_one.setOnClickListener(myListener);

        pay_two = (TextView) findViewById(R.id.pay_two);
        pay_two.setOnClickListener(myListener);

        pay_three = (TextView) findViewById(R.id.pay_three);
        pay_three.setOnClickListener(myListener);

        pay_four = (TextView) findViewById(R.id.pay_four);
        pay_four.setOnClickListener(myListener);

        pay_five = (TextView) findViewById(R.id.pay_five);
        pay_five.setOnClickListener(myListener);

        pay_six = (TextView) findViewById(R.id.pay_six);
        pay_six.setOnClickListener(myListener);

        pay_seven = (TextView) findViewById(R.id.pay_seven);
        pay_seven.setOnClickListener(myListener);

        pay_eight = (TextView) findViewById(R.id.pay_eight);
        pay_eight.setOnClickListener(myListener);

        pay_nine = (TextView) findViewById(R.id.pay_nine);
        pay_nine.setOnClickListener(myListener);

        pay_zero = (TextView) findViewById(R.id.pay_zero);
        pay_zero.setOnClickListener(myListener);
        pay_space = (TextView) findViewById(R.id.pay_space);
        pay_space.setOnClickListener(myListener);
        //清除
        pay_del = (TextView) findViewById(R.id.pay_del);
        pay_del.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                moneytext = money.getText().toString();
                int index = moneytext.length();
                if (index > 0) {

                    moneytext = moneytext.substring(0, moneytext.length() - 1);
                    money.setText(moneytext);
                }

            }
        });

        //确认
        pay_affirm.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                if (TextUtils.isEmpty(money.getText().toString())) {
                    Toast.makeText(PayByCashActivity.this, "请输入实收金额", Toast.LENGTH_SHORT).show();
                    return;
                }

                String needPay = handleone.getText().toString().trim();
                double needPayDoube = 0;
                if (!TextUtils.isEmpty(needPay)) {
                    needPayDoube = Double.parseDouble(needPay);
                }
                double moneyDouble = Double.parseDouble(money.getText().toString().trim());
                Log.v("zyl", "needPayDouble:" + needPayDoube);
                Log.v("zyl", "moneyDouble:" + moneyDouble);

                if (moneyDouble < needPayDoube) {
                    Toast.makeText(PayByCashActivity.this, "实收金额必须大于应付金额", Toast.LENGTH_SHORT).show();
                    return;
                }


                //找零多少
                String changestr = change.getText().toString();
                Log.v("zyl", "changestr:" + changestr);
                if (changestr != "") {
                    if (Double.parseDouble(changestr.replace("¥", "")) >= 0) {
                        pay_affirm.setClickable(false);
                        show_send_coupon();



//                        handler.sendEmptyMessage(3);
                    }
                }
            }
        });

    }


    /**
     * 弹出增送礼券的对话框
     */
    public void show_send_coupon() {

            GoBackToSumActivity(3,"");

    }




    private void GoBackToSumActivity(int handle,String error_msg){
//        Intent intent=new Intent();
//        intent.setClass(PayByCashActivity.this,SumNewActivity.class);
//        intent.putExtra("handle",handle);
//        intent.putExtra("msg",error_msg);
//        startActivity(intent);
//        finish();

        Intent intent = new Intent();
        String zhaoling=change.getText().toString().replace("¥", "");//找零
            if(TextUtils.isEmpty(zhaoling)){
                zhaoling="0.00";
            }
            intent.putExtra("change",zhaoling);

        intent.setClass(PayByCashActivity.this,SumNewActivity.class);
        setResult(Activity.RESULT_OK, intent);
        finish();
    }



    View.OnClickListener myListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            moneytext = money.getText().toString();
            if (moneytext.contains(".")) {
                String[] tempS = moneytext.split("\\.");
                if (tempS.length > 1) {
                    int resultNum = tempS[1].length();
                    if (resultNum > 1) {
                        return;
                    }
                }
            }
            switch (v.getId()) {
                case R.id.pay_space:
                    moneytext = money.getText().toString();
                    if (!moneytext.contains(".")) {
                        if (moneytext.equals("")) {
                            input_digit("0.");
                        } else {
                            input_digit(".");
                        }
                    }
                    break;

                case R.id.pay_zero:
                    input_digit("0");

                    break;
                case R.id.pay_one:
                    input_digit("1");

                    break;
                case R.id.pay_two:
                    input_digit("2");

                    break;
                case R.id.pay_three:
                    input_digit("3");

                    break;
                case R.id.pay_four:
                    input_digit("4");

                    break;
                case R.id.pay_five:
                    input_digit("5");

                    break;
                case R.id.pay_six:
                    input_digit("6");

                    break;
                case R.id.pay_seven:
                    input_digit("7");

                    break;
                case R.id.pay_eight:
                    input_digit("8");

                    break;
                case R.id.pay_nine:
                    input_digit("9");

                    break;
                default:
                    break;
            }
        }
    };

    private void input_digit(String digit) {
        moneytext = money.getText().toString();
        if (moneytext.indexOf(".") != -1) {
            //获取小数点的位置
            int num = 0;
            num = moneytext.indexOf(".");
            if (moneytext.length() - num > 2) {

            } else {
                moneytext = moneytext + digit;
            }
        } else {
            moneytext = moneytext + digit;
        }
        money.setText(moneytext);
    }

    private double subtractDouble(double d1, double d2) {
        BigDecimal original = new BigDecimal(d1);
        BigDecimal discount = new BigDecimal(d2);
        BigDecimal moneyDecimal = original.subtract(discount);
        //保留2位小数
        double moneyF = moneyDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return moneyF;
    }

    private ProgressDialog loadingDialog;   //结算中弹出框

    /**
     * 显示结算中弹出框
     */
    public void showLoadingDalog(){
        if(loadingDialog==null)
            loadingDialog = new ProgressDialog(PayByCashActivity.this);
        loadingDialog.setCancelable(false);
        loadingDialog.setMessage("结算中...");
        loadingDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        loadingDialog.setProgress(0);
        loadingDialog.setMax(100);
        loadingDialog.show();
    }


}
